package UFFT_package.Decision1;

import java.io.File;
import java.io.FileReader;
import java.util.*;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import UFFT_package.Dimension1.Dimension;
import UFFT_package.Example1.Example;
import UFFT_package.Node1.Node;

public class Decision {

	String data;
	public static int num_classes;
	public static String[] classes;
	public static Dimension[] dimensions;
	public static int num_dimensions;
	public static int num_examples;
	Example example;
    public static ArrayList l1=new ArrayList(); 
    int valid;
    public static int n_min=5;
    public static int majority_class;
    int majority_class_count;
    static Node root;
    public static DefaultMutableTreeNode top;
	public static Integer[] class_count;   
    public static String[] all_d=new String[10000];
    public static int count;
	//public static int current_index,previous_index;
    public static Stack<Double> stck= new Stack<Double>();
    public Decision(){}
    public Decision(String fname) throws Exception{
	    
		String temp;
		FileReader fr = null;
		try
		{
		    
			File file=new File(fname);
			Scanner sc=new Scanner(file);
			
			temp=sc.next();
			//System.out.println(temp);
			if(!temp.equals("classes"))
			{
				System.out.println("Error : Keyword classes required..");
			}
			System.out.print("ClassLabel : ");
			num_classes=sc.nextInt();
			System.out.print(num_classes);
            classes=new String[num_classes];  //allocating memory
           	class_count=new Integer[num_classes]; 
            int i;
			for(i=0;i<num_classes;i++) //store classes in the array
			{
				classes[i]=sc.next();
				System.out.println(classes[i]+" ");
			  class_count[i]=0;
			}
			
			temp=sc.next();
			if(!temp.equals("dimensions"))
			{
				System.out.println("Error : Keyword dimensions required..");
			}
			
			System.out.println("Attributes : ");
			num_dimensions =sc.nextInt();
			dimensions=new Dimension[num_dimensions]; //allocate memory
			for(i=0;i<num_dimensions;i++)
			{
				dimensions[i]= new Dimension();
				dimensions[i].setAttribute(sc.next());
			    int num=sc.nextInt();
				dimensions[i].setNum_values(num);
			    dimensions[i].print_data();  	
			}
			root=new Node();
			for(int x=0;x<num_dimensions;x++)
			     root.add_attribute(x);
			temp=sc.next();
			if(!temp.equals("Examples"))
			{
				System.out.println("Error : Keyword examples required..");
			}
			System.out.println("\nExamples : ");
			num_examples=sc.nextInt();
			for(i=0;i<num_examples;i++)
			{
				example=new Example(num_dimensions);
				
				for(int x=0;x<num_dimensions;x++)
				{
                     example.values[x]=sc.next();    
				}
				temp=sc.next();
				for(int j=0;j<num_classes;j++)
				{
					if(temp.equals(classes[j])){
						example.my_class=j;
					class_count[j]++;
					}
				}
				l1.add(example);
				
				root.add_example(example);
			}
			
			for(int j=0;j<num_classes;j++)
			{
				System.out.print(class_count[j]+" ");
			}
			
			Iterator it = l1.iterator();
	    	while(it.hasNext())
	    	{
	    		Example e=(Example) it.next();
	    	  		
	    		for(i=0;i<num_dimensions;i++)
	    		{
	    		
	    			valid=dimensions[i].compare(e,i);
	    			
	    			if(valid==0)
		    			System.out.println("Example invalid.");
	    		}
	    		int j;
	    		for(j=0;j<num_classes;j++)	
    			{
    				if(e.my_class==j)
    					break;
       			}
	    		if(j>=num_classes)
	    			System.out.println("Class invalid.");
	    		
	    		for(int x=0;x<num_dimensions;x++)
				{
                     System.out.print(e.values[x]+" ");    
				}
				System.out.print(e.my_class+"\n");
		    		
	    	}
	    	
	    	sc.close();

	    	
	    	majority_class_count=class_count[0];
	    	for(int x=0;x<num_classes;x++)
	    	{
	    		if(class_count[x]>majority_class_count)
	    		{
	    			majority_class=x;
	    			majority_class_count=class_count[x];
	    		}
	    	}
	    	System.out.println("Majority Class : " + majority_class);
	    	System.out.println("\n\n**CLASSIFICATION OF ATTRIBUTES");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			
		}
	}
    public static void store_pos_value_left(int i,double d)
    {
    	stck.push(d);
    	//System.out.println("Left:  " + d +"\t"+ count+"\t  Stack size " + stck.size());
    	all_d[count]="<="+d;
 	   	count++;  
    //	current_index=count-1;
    	
 	   	   	
    }

    public static void store_pos_value_right()
    {
    	
    	Double temp1 = -1.0;
    	
    	if(stck.empty()==false)
    		temp1=stck.pop();
    	else
    		return;
    	
    	all_d[count]=">"+temp1;
    	count++;
    	
     }
    
	public void build_tree() 
	{
		
		root.build_tree(majority_class,num_classes);
		
	}

	public static JInternalFrame print() 
	{
		System.out.println("Decision Tree-1");  
		//count=0;
		//System.out.println("----------------------------"+root.attribute);
		count=0;
		
		if(root.attribute!=-1)
		{	
			top=new DefaultMutableTreeNode(dimensions[root.attribute].getAttribute(),true); //it will take root of d decision tree(i.e. period for 27 records)
			root.print(top,0);
			JScrollPane spane,sp1;
			
			JTextArea a;
			JInternalFrame tree_frame1=new JInternalFrame("Decision Tree using UFFT");
			String str = null;
			JTree tree=new JTree(top);			
		    spane = new JScrollPane(tree);
		    spane.getHorizontalScrollBar().addAdjustmentListener(null);
		    spane.getVerticalScrollBar().addAdjustmentListener(null);
		    tree_frame1.add(spane);
		    tree_frame1.setSize(400,400);
		    tree_frame1.setVisible(true);  
		    return tree_frame1;
		}
		return null;
	}
	
	public static String traverse(Example testData)
	{
		String str;
		DefaultMutableTreeNode t1=new DefaultMutableTreeNode();
	    t1=top;
	    //System.out.println("traversing Tree");
	    
	    for(int i=0;i<num_dimensions;i++)
	    {
	    	str=t1.toString();
	    	//System.out.println(" *****  "+str);
	    	int position=lookUpAttribute(str);
	    	Enumeration en = t1.children();
	    	
	    	if(en.hasMoreElements() == false )
	    		continue;
	    	
	    	if(t1.getChildCount()==1)
	    	{
	    		t1=(DefaultMutableTreeNode) t1.getFirstChild();
	    		break;
	    	}
	    	else
	    	{
	    		DefaultMutableTreeNode node = (DefaultMutableTreeNode) en.nextElement();
	    		//System.out.println("***"+node.toString());
	    		double at1=Double.parseDouble(testData.values[position]);// comparing ech attribut value to check @ which child does the eg. lie
	    		String str1=node.toString();
	    		str1=str1.substring(2, str1.length());
	    		//System.out.println("*****"+str1);
	    		double at2=Double.parseDouble(str1);
			    
	    		if(at1<=at2)  
	    		{ 
			    	  t1=(DefaultMutableTreeNode) node.getFirstChild();
			    	  
			    }
	    		else
	    		{
	    			 node = (DefaultMutableTreeNode) en.nextElement();
	    			 t1=(DefaultMutableTreeNode) node.getFirstChild();
	    		}
	    		
	    	}
	    }
	    
	    //System.out.println("Class Label:" +t1.toString());
		return t1.toString();
	}

	private static int lookUpAttribute(String str) 
	{
		for(int i=0;i<num_dimensions;i++)
		{
			if(dimensions[i].attribute.equals(str))
				return i;
			
		}
		return -1;	
	}
	
 }
